package com.uit.se6.customui.android.navigation.tabview;

import java.util.List;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;

import com.uit.se6.customui.R;

public class TabsPager extends ViewPager implements ActionBar.TabListener {
	private int header_color;
	private boolean initialized;
	private List<Fragment> listTab;

	public TabsPager(Context context, AttributeSet attrs) {
		super(context, attrs);

		if (attrs != null) {
			try {
				TypedArray ta = context.obtainStyledAttributes(attrs,
						R.styleable.TabsPager);

				// headers
				header_color = ta.getResourceId(
						R.styleable.TabsPager_header_color, 0);

				ta.recycle();
			} catch (Exception e) {

				Log.e(this.getClass().getName(),
						"Exception when obtaining TypedArray: " + e.toString());
			}
		}

	}

	private ActionBar actionBar;
	private TabsPagerAdapter adapter;
	private ColorDrawable colorDrawable;

	public void setup(FragmentActivity act, List<Fragment> list) {
		colorDrawable = new ColorDrawable(Color.parseColor("#ffFEBB31"));
		actionBar = act.getActionBar();      
		listTab = list;
		adapter = new TabsPagerAdapter(act.getSupportFragmentManager(), listTab);
		this.setAdapter(adapter);
		actionBar.setHomeButtonEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		for (int i = 0; i < listTab.size(); i++) {
			actionBar.addTab(actionBar.newTab().setText("ex" + i)
					.setTabListener(this));
		}
		actionBar.setBackgroundDrawable(colorDrawable);
		actionBar.setStackedBackgroundDrawable(colorDrawable);
		this.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// on changing the page
				// make respected tab selected
				actionBar.setSelectedNavigationItem(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}

	@Override
	public void onFinishInflate() {
		if (initialized) {
			super.onFinishInflate();
			return;
		}
		//

		initialized = true;
		super.onFinishInflate();
		
	}

	@Override
	public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction arg1) {
		this.setCurrentItem(tab.getPosition());

	}

	@Override
	public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub

	}

}
